package com.ciaojian.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciaojian.client.service.CouponCodeServiceImpl;
import com.ciaojian.client.service.CouponCodeTakeRecordServiceImpl;
import com.ciaojian.client.service.MarketingScanServiceImpl;
import com.ciaojian.core.enums.business.CouponCodeTakeRecordTypeEnum;
import com.ciaojian.core.model.CouponCodeTakeRecord;
import com.ciaojian.core.model.CouponPackage;
import com.ciaojian.core.model.MarketingScan;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author Atlakyn
 */
@Validated
@Api(tags = "扫码领取记录")
@RestController
@RequestMapping("/market-scan-record")
public class MarketScanRecordController {
    @Resource
    private CouponCodeTakeRecordServiceImpl couponCodeTakeRecordService;
    @Resource
    private MarketingScanServiceImpl marketingScanService;
    @Resource
    private CouponCodeServiceImpl couponCodeService;

    @ApiOperation("领券")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    public Result takeUp(@NotNull(message = "扫码页面id不能为空") Integer marketScanId,
                         @NotNull(message = "用户id不能为空") Integer userId) {
        LambdaQueryWrapper<CouponCodeTakeRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CouponCodeTakeRecord::getActivityId, marketScanId)
                .eq(CouponCodeTakeRecord::getUserId, userId)
                .eq(CouponCodeTakeRecord::getType, CouponCodeTakeRecordTypeEnum.MARKET_SCAN);
        int count = couponCodeTakeRecordService.count(lambdaQueryWrapper);
        Assert.isTrue(count == 0, "抱歉您已领取过");

        MarketingScan marketingScan = marketingScanService.getById(marketScanId);
        switch (marketingScan.getCoupon()) {
            // 0是领券
            case 0:
                couponCodeService.advTakeCode(userId, marketingScan.getCouponId(), marketingScan.getId(), CouponCodeTakeRecordTypeEnum.MARKET_SCAN);
                break;
            // 0是领券包
            case 1:
                // 获取券包下的所有券
                CouponPackage couponPackage = new CouponPackage().selectById(marketingScan.getCouponId());
                String coupons = couponPackage.getCoupons();
                String[] split = coupons.split(",");
                for (String s : split) {
                    couponCodeService.advTakeCode(userId, Integer.valueOf(s), marketingScan.getId(), CouponCodeTakeRecordTypeEnum.MARKET_SCAN);
                }
                break;
            default:
                break;
        }
        return Result.ok();
    }
}
